package top.blackr.folivora.common.security.crud.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import top.blackr.folivora.common.core.R;
import top.blackr.folivora.common.log.annotation.SysLog;
import top.blackr.folivora.common.security.crud.model.vo.SysMenuVo;
import top.blackr.folivora.common.security.crud.repository.SysMenuRepository;

import java.util.List;

@Tag(name = "菜单")
@RestController
@AllArgsConstructor
@RequestMapping("menu")
public class SysMenuController {

    private final SysMenuRepository menuService;

    @GetMapping("page")
    @Operation(summary = "树状信息", description = "树状信息")
    @SaCheckPermission("menu_read")
    @Parameters({
            @Parameter(schema = @Schema(name = "lazy", description = "是否懒加载:true/false", defaultValue = "true")),
            @Parameter(schema = @Schema(name = "parentId", description = "父级id", defaultValue = "0"))
    })
    public R<List<Tree<Integer>>> getMenuPage(boolean lazy, Integer parentId) {
        return R.ok(menuService.treeMenu(lazy, parentId));
    }

    /**
     * 根据登录用户的角色权限返回菜单信息
     *
     * @return 菜单列表
     */
    @GetMapping("menuAuthority")
    @Operation(summary = "根据登录用户的角色权限返回菜单信息")
    public R<?> menuAuthority() {
        return R.ok(menuService.menuAuthority());
    }

    @SysLog("'新增菜单 ' + #params.name")
    @PostMapping("save")
    @Operation(summary = "新增菜单")
    @SaCheckPermission("menu_write")
    public R<Boolean> saveMenu(@RequestBody SysMenuVo params) {
        return R.ok(menuService.saveMenu(params));
    }

    @SysLog("'更新菜单 ' + #params.menuId")
    @PostMapping("update")
    @Operation(summary = "更新菜单")
    @SaCheckPermission("menu_write")
    public R<Boolean> updateMenu(@RequestBody SysMenuVo params) {
        return R.ok(menuService.updateMenu(params));
    }

    @SysLog("'删除菜单 ' + #params.menuId")
    @PostMapping("delete")
    @Operation(summary = "删除菜单")
    @SaCheckPermission("menu_del")
    public R<Boolean> delMenu(@RequestBody SysMenuVo params) {
        return R.ok(menuService.delMenu(params));
    }

}
